﻿using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models.DBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using jiaju.Areas.Back.Models;
namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class ProductTypeController : Controller
    {
        // GET: Back/ProductType
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult getProductType(int page, int limit, string name)
        {
            //调用Service里的方法
            List<ProductType> dataList = ProductTypeService.getProductTypeList(page,limit, name);
            //获取总条数
            string sql = "select COUNT(*) from dbo.productType";
            if (!string.IsNullOrEmpty(name))
            {              
                sql = sql + string.Format(" where Name like '%{0}%'", name);
            }        
            int cout = (int)DBHelp.ExcuteScalar(sql);
            Object obj = new
            {
                code = 0,
                msg = "请求成功",
                count = cout,
                data = dataList
            };
            return Json(obj, JsonRequestBehavior.AllowGet);
        }
        //添加商品分类页面   
        public ActionResult Add()
        {
            return View();
        }
        public ActionResult AddDel(ProductType art)
        {
            //新增 请求数据库
            int n = ProductTypeService.AddProductType(art);
            if (n > 0)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }
        //编辑商品分类
        public ActionResult Edit(string Id)
        {
            //通过Id获取数据详情
            ProductType art = ProductTypeService.GetById(Id);
            //把详情数据传到页面
            ViewBag.A = art;
            return View();
        }
        [HttpPost]
        public ActionResult EditDel(ProductType art)
        {
            //编辑，请求数据库
            int m = ProductTypeService.EditProductType(art);
            if (m > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        //删除商品分类方法DelArt
        public ActionResult DelArt(String Id)
        {
            int m = ProductTypeService.DelProductType(Id);
            if (m > 0)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
        }
    }
}